﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002E9 RID: 745
	internal class SelectObservable<T, TR> : OperatorObservableBase<TR>, ISelect<TR>
	{
		// Token: 0x06000FCE RID: 4046 RVA: 0x00049134 File Offset: 0x00047534
		public SelectObservable(IObservable<T> source, Func<T, TR> selector) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.selector = selector;
		}

		// Token: 0x06000FCF RID: 4047 RVA: 0x00049150 File Offset: 0x00047550
		public SelectObservable(IObservable<T> source, Func<T, int, TR> selector) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.selectorWithIndex = selector;
		}

		// Token: 0x06000FD0 RID: 4048 RVA: 0x0004916C File Offset: 0x0004756C
		public IObservable<TR> CombinePredicate(Func<TR, bool> predicate)
		{
			if (this.selector != null)
			{
				return new SelectWhereObservable<T, TR>(this.source, this.selector, predicate);
			}
			return new WhereObservable<TR>(this, predicate);
		}

		// Token: 0x06000FD1 RID: 4049 RVA: 0x00049193 File Offset: 0x00047593
		protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
		{
			if (this.selector != null)
			{
				return this.source.Subscribe(new SelectObservable<T, TR>.Select(this, observer, cancel));
			}
			return this.source.Subscribe(new SelectObservable<T, TR>.Select_(this, observer, cancel));
		}

		// Token: 0x04000938 RID: 2360
		private readonly IObservable<T> source;

		// Token: 0x04000939 RID: 2361
		private readonly Func<T, TR> selector;

		// Token: 0x0400093A RID: 2362
		private readonly Func<T, int, TR> selectorWithIndex;

		// Token: 0x020002EA RID: 746
		private class Select : OperatorObserverBase<T, TR>
		{
			// Token: 0x06000FD2 RID: 4050 RVA: 0x000491C7 File Offset: 0x000475C7
			public Select(SelectObservable<T, TR> parent, IObserver<TR> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000FD3 RID: 4051 RVA: 0x000491D8 File Offset: 0x000475D8
			public override void OnNext(T value)
			{
				TR value2 = default(TR);
				try
				{
					value2 = this.parent.selector(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.observer.OnNext(value2);
			}

			// Token: 0x06000FD4 RID: 4052 RVA: 0x00049250 File Offset: 0x00047650
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FD5 RID: 4053 RVA: 0x00049288 File Offset: 0x00047688
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400093B RID: 2363
			private readonly SelectObservable<T, TR> parent;
		}

		// Token: 0x020002EB RID: 747
		private class Select_ : OperatorObserverBase<T, TR>
		{
			// Token: 0x06000FD6 RID: 4054 RVA: 0x000492C0 File Offset: 0x000476C0
			public Select_(SelectObservable<T, TR> parent, IObserver<TR> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.index = 0;
			}

			// Token: 0x06000FD7 RID: 4055 RVA: 0x000492D8 File Offset: 0x000476D8
			public override void OnNext(T value)
			{
				TR value2 = default(TR);
				try
				{
					value2 = this.parent.selectorWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.observer.OnNext(value2);
			}

			// Token: 0x06000FD8 RID: 4056 RVA: 0x00049364 File Offset: 0x00047764
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000FD9 RID: 4057 RVA: 0x0004939C File Offset: 0x0004779C
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400093C RID: 2364
			private readonly SelectObservable<T, TR> parent;

			// Token: 0x0400093D RID: 2365
			private int index;
		}
	}
}
